package com.zyb.system.systemdictionary.controller;

import com.zyb.system.systemdictionary.domain.SystemDictionaryDetail;
import com.zyb.system.systemdictionary.query.SystemDictionaryDetailQuery;
import com.zyb.system.systemdictionary.service.ISystemDictionaryDetailService;
import com.zyb.utils.AjaxResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/detail")
@Api(value = "员工的API",description="员工相关的CRUD功能")
public class DetailController {
    @Autowired
    private ISystemDictionaryDetailService systemDictionaryDetailService;

    @GetMapping("/list")//方式二
    @ApiOperation(value = "查询所有员工所有数据")
    public List<SystemDictionaryDetail> queryAll(){
        return systemDictionaryDetailService.queryAll();
    }

    @PostMapping("/{id}")
    @ApiOperation(value = "查询部门的一条数据")
    //required = true 表示必填项
    public AjaxResult queryOne(@PathVariable Long id){
        try {
            SystemDictionaryDetail data = systemDictionaryDetailService.queryById(id);
            return AjaxResult.success().setData(data);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error().setMsg("获取数据失败");
    }

    @PostMapping("/pageList")
    @ApiOperation(value = "字典分页查询")
    public AjaxResult pageList(@RequestBody SystemDictionaryDetailQuery query){

        try {
            List<SystemDictionaryDetail> page = systemDictionaryDetailService.queryPage(query);
            Long count = systemDictionaryDetailService.queryCount(query);
            return AjaxResult.success().setData(page).setTotal(count);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error().setMsg("获取分页数据失败");
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "根据id删除一条数据")
    public AjaxResult deleteById(@PathVariable Long id){
        try {
            systemDictionaryDetailService.remove(id);
            return AjaxResult.success().setMsg("删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }
    }

    @PutMapping
    @ApiOperation(value = "新增【如有id为修改操作】")
    public AjaxResult addOrUpdate(@RequestBody SystemDictionaryDetail systemDictionaryDetail){
        try {
            if(systemDictionaryDetail.getId() == null){
                systemDictionaryDetailService.add(systemDictionaryDetail);
                return AjaxResult.success().setMsg("添加成功");
            }else {
                systemDictionaryDetailService.update(systemDictionaryDetail);
                return AjaxResult.success().setMsg("修改成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return AjaxResult.error().setMsg("保存数据失败");
    }

    @PatchMapping("/del")
    @ApiOperation(value = "根据id批量删除")
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
            systemDictionaryDetailService.batchDelete(ids);
            return AjaxResult.success().setMsg("批量删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error().setMsg(e.getMessage());
        }
    }
}
